package utils

import (
	"encoding/json"
	"fmt"
	"os"
)

type ExecResult struct {
	Ok   bool `json:"ok"`
	Data any  `json:"data"`
}

func OutputError(execErr error) {
	obj := &ExecResult{
		Ok:   false,
		Data: execErr.Error(),
	}
	data, err := json.Marshal(obj)
	if err == nil {
		os.Stderr.WriteString(string(data))
		os.Stderr.WriteString("\n")
	}
	os.Exit(1)
}

func OutputData(result any) {
	obj := &ExecResult{
		Ok:   true,
		Data: result,
	}
	data, err := json.Marshal(obj)
	if err == nil {
		fmt.Println(string(data))
	}
}
